#include <iostream>
#include <cstring>

char *myStrtok(char *source, const char *delim)
{
    static char *delimHeader = nullptr;
    if (source == nullptr)
    {
        if (delimHeader == nullptr)
        {
            return nullptr;
        }
        source = delimHeader;
    }
    char *target = strstr(source, delim);
    if (target == nullptr)
    {
        delimHeader = nullptr;
        return source;
    }
    else
    {
        *target = '\0';
        delimHeader = target + strlen(delim);
        return source;
    }
}
/*根据delim分割字符串source*/
char *myStrtok_01(char *source, const char *delim)
{
    static char *nextStart = nullptr;

    if (source == nullptr)
    {
        if (nextStart == nullptr)
        {
            return nullptr;
        }
        source = nextStart;
    }
    char *record = strstr(source, delim);

    if (record != nullptr)
    {
        *record = '\0';
        nextStart = record + strlen(delim);
        return source;
    }
    else
    {
        nextStart = nullptr;
        return source;
    }
}
int main()
{
    char str[] = "zhangsan,lisi,wangwu";
    char *s = myStrtok_01(str, ",");
    while (s != nullptr)
    {
        std::cout << s << std::endl;
        s = myStrtok_01(nullptr, ",");
    }
    // s = myStrtok(NULL, ",");
    // s = myStrtok(NULL, ",");
    // s = myStrtok(NULL, ",");
    // std::cout << s << std::endl;
    // std::cout << &str << " " << &s << std::endl;
}